package com.wenhai.spring.aop.after;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;

public class SimpleThrowsAdvice implements ThrowsAdvice {


    public static void main(String[] args) {
        ErrorBean errorBean = new ErrorBean();
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(errorBean);
        proxyFactory.addAdvice(new SimpleThrowsAdvice());
        ErrorBean proxy = (ErrorBean) proxyFactory.getProxy();
        try {
            proxy.errorProneMethod();
        } catch (Exception e) {
        }
        try {
            proxy.otherErrorProneMethod();
        } catch (IllegalArgumentException e) {
        }

    }

    public void afterThrowing(Exception ex) {
        System.out.println("***");
        System.out.println("Generic Exception Capture");
        System.out.println("Caught:" + ex.getClass().getName());
        System.out.println("***");
    }

    public void afterThrowing(Method method, Object[] args, Object target, IllegalArgumentException ex) {

        System.out.println("***");
        System.out.println("IllegalArgumentException Capture");
        System.out.println("Caught:" + ex.getClass().getName());
        System.out.println("Method: " + method.getName());
        System.out.println("***");
    }



}
